#include "selectmethod.h"
#include "ui_selectmethod.h"

SelectMethod::SelectMethod(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SelectMethod)
{
    ui->setupUi(this);
    //ui美化
    this->initStyleSheet();
}

SelectMethod::~SelectMethod()
{
    delete ui;
}

void SelectMethod::initStyleSheet()
{
    QString qss;

    qss += "QLineEdit{border-width: 1px; border-radius: 10px; border-style: solid; border-color: rgb(98,183,152); }";
    /*
    qss += "QPushButton:hover{background-color:rgb(229,229,229); color:black;}";
    qss += "QPushButton:pressed,QPushButton:checked{background-color:rgb(0,118,215);}";
    qss += "#pushButton_closeKeyboard{background-color:rgba(0,0,0,0); border:0px}";
    qss += "#pushButton_closeKeyboard:hover{background-color:#b30220;}";
    qss += "#pushButton_space{min-width:500px;}";
    qss += "#pushButton_backspace,#pushButton_shift{min-width:100px;}";
    qss += "#pushButton_enter{min-width:120px;}";
    qss += "#pushButton_tab,#pushButton_ctrl{min-width:70px;}";
    qss += "#pushButton_capsLock{min-width:80px;}";
    qss += "#pushButton_up{min-width:150px;}";
    */
    qss += "QHeaderView::section {"
                   "background-color: black;"
                   "border: 1px solid gray;"
                   "height: 5px; /* 调整高度 */"
                   "}";
    this->setStyleSheet(qss);
}


void SelectMethod::on_confirm_clicked()
{
    emit methodName(ui->listWidget->currentItem()->text());//将目标方法名传送给测试页面
    emit display(2); //t
}


void SelectMethod::on_home_clicked()
{
    Permission::LastPage = 1;
    emit display(5); //l2
}


void SelectMethod::on_methoddevelop_clicked()
{
    emit display(3); //m
}


void SelectMethod::on_settings_clicked()
{
    emit display(4); //set
}

//方法列表显示
void SelectMethod::resetListWidget()
{
    //将该用户所拥有的方法显示在列表中(更新列表)
    ui->listWidget->clear();
    MethodManager methodManager;
    QVector<Method> allMethod;
    if(Permission::User == "admin"){
        allMethod = methodManager.getAllMethod();
    }else{
        allMethod = methodManager.getMethodByUser(Permission::User);
    }

    for (const Method& method : allMethod) {
        qDebug() << "Methodname:" << method.methodName;
        ui->listWidget->addItem(method.methodName);
    }
}



void SelectMethod::on_deleteconfirm_clicked()
{
    MethodManager methodManager;

    bool success = methodManager.deleteMethod(ui->listWidget->currentItem()->text());
    resetListWidget();//刷新列表
    if (success) {
        QMessageBox::warning(this,tr("delete success"),tr("delete sucess！"),QMessageBox::Ok);
        qDebug() << "Method deleted successfully";
    } else {
        QMessageBox::warning(this,tr("delete failed"),tr("delete failed！"),QMessageBox::Ok);
        qDebug() << "Failed to delete method";
    }
}


void SelectMethod::hiding(bool s)
{
    //管理员相关管理
    if(s==0){
        ui->settings->hide();
        ui->deleteconfirm->hide();
    }else{
        ui->settings->show();
        ui->deleteconfirm->show();
    }
}

